
// 注册
bangkeapp.controller('bangkelogin', ["$scope", "registerService", "$interval",
    function($scope, registerService, $interval) {
    		// 获取html的参数
    		var url = location.search; //获取url中"?"符后的字串 
		var theRequest = new Object(); 
		if (url.indexOf("?") != -1) { 
			var base = new Base64();  
			var str = base.decode(url.substr(1));  
        		
			strs = str.split("&"); 
			for(var i = 0; i < strs.length; i ++) { 
				theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]); 
			}
			$scope.register = {};
			$scope.register.reference = theRequest.reference;
		}
		
		$scope.smscode = "";
		$scope.text = "获取验证码";
		// 获取验证码
		$scope.getsmscode = function(){
			
			if (undefined == $scope.register.tel || "" == $scope.register.tel) {
				alert("手机号不能为空!");
				return false;
			}
			var myreg = /^1[1|2|3|4|5|6|7|8|9|0][0-9]{9}$/; 
			if(!myreg.test($scope.register.tel)) { 
			    alert('不是有效手机号！'); 
			    return false; 
			} 
			$scope.tel = $scope.register.tel;
			
			
			$scope.n = 60;
	        $scope.text = $scope.n + "秒";
	        $scope.isDisable = true;
	
	        var time = $interval(function () {
	
	            $scope.n--;
	            $scope.text = $scope.n + "秒后可重新获取";
	            if ($scope.n == 0)
	            {
	                $interval.cancel(time);
	                $scope.isDisable = false;
	                $scope.text = "获取验证码";
	            }
	
	        },1000);

			
			registerService.getsmscode($scope.tel).then(function(result) {
	            var data = result;
	            if(data.Response.Header.ResponseCode == "M"){
	            		$scope.smscode = data.Response.Body.Response[0].smscode;
	            } else {
	            		alert(data.Response.Header.MsgList[0].MsgContent + " 请重新获取验证码!");
	            }
	        }, function(error) {
	            console.log("error!");
	        }, function(progress) {
	
	        });
		}
		
		// 跳转到修改查看会员信息界面
		$scope.tologin = function(){
			// 校验
			if ($scope.smscode != $scope.register.verificationcode) {
				alert("验证码错误!");
				return false;
			}
			if (undefined == $scope.register.membername || "" == $scope.register.membername) {
				alert("用户名不能为空!");
				return false;
			}
			if (undefined == $scope.register.password || "" == $scope.register.password) {
				alert("密码不能为空!");
				return false;
			}
			if ($scope.register.password.length < 8) {
				alert("密码必须大于8位!");
				return false;
			}
			if (undefined == $scope.register.repassword || "" == $scope.register.repassword) {
				alert("确认密码不能为空!");
				return false;
			}
			if (undefined == $scope.register.idcard || "" == $scope.register.idcard) {
				alert("身份证号不能为空!");
				return false;
			}
			if (undefined == $scope.register.tel || "" == $scope.register.tel) {
				alert("手机号不能为空!");
				return false;
			}
			if ($scope.register.membername.length > 11) {
				alert("用户名长度不能大于11位!");
				return false;
			}
			
			registerService.register($scope.register).then(function(result) {
	            var data = result;
	            if(data.Response.Header.ResponseCode == "M"){
	            		window.location.href = "registerok.html";
	            } else {
	            		alert(data.Response.Header.MsgList[0].MsgContent);
	            }
	        }, function(error) {
	            console.log("error!");
	        }, function(progress) {
	
	        });
		}
    }
]);